package com.zmn.brs.task.job;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.brs.business.interfaces.engineer.EngineerSalesBService;
import com.zmn.brs.business.interfaces.engineer.EngineerUpaidBService;
import com.zmn.brs.business.interfaces.event.EventBService;
import com.zmn.brs.common.constant.TaskConst;
import com.zmn.brs.common.enums.EventTypeEnum;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：工程师监控规则定时任务
 *
 * @author chongw
 * @since 2021/07/08 11:12
 */
@LTS
@Slf4j
public class EngineerRuleJob {

    private static final String TAG = "工程师监控规则定时任务触发类";

    @NacosValue(value = "${sms.ruleId.dingTalk.engineer}", autoRefreshed = true)
    Integer smsRuleId;

    @NacosValue(value = "${spring.mq.topic.push}", autoRefreshed = true)
    String pushTopic;

    @NacosValue(value = "${cube.snapshot.default.staffId}", autoRefreshed = true)
    Integer staffId;

    @Resource
    private EventBService eventBService;

    @Resource
    private EngineerSalesBService engineerSalesBService;

    @Resource
    private EngineerUpaidBService engineerUpaidBService;

    @Resource
    private TaskManager taskManager;

    @PostConstruct
    public void init() {
        log.info("[{}], 工程师监控数据推送定时任务初始化提交", TAG);
        //初始化派单客单价触发任务
        eventBService.initEventTypeJob(EventTypeEnum.ENGINEER_SALES.getTypeId());

//        Job unpaidPush = new Job();
//        unpaidPush.setTaskId(TaskConst.TASK_ID_BRS_ENGINEER_UNPAID_PUSH);
//        unpaidPush.setCronExpression("0 */30 * * * ?");
//        unpaidPush.setReplaceOnExist(true);
//        unpaidPush.setParam(TaskManager.SHARD_FIELD, TaskConst.TASK_ID_BRS_ENGINEER_UNPAID_PUSH);
//        taskManager.submitTask2(unpaidPush);

        log.info("[{}], 工程师监控数据推送定时任务初始化成功......", TAG);
    }

    /**
     * 描述: 派单客单价
     *
     * @param job 任务
     * @return Result
     * @author chongw
     * @since 2021/07/08 11:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_ENGINEER_SALES)
    public Result salesJob(Job job) {
        try {
            log.info("工程师派单客单价====开始===");
            Boolean result = engineerSalesBService.compute(EventTypeEnum.ENGINEER_SALES.getTypeId(), staffId, smsRuleId, pushTopic);
            log.info("工程师派单客单价====结束===");
            if (result) {
                return new Result(Action.EXECUTE_SUCCESS, "工程师监控推送任务成功");
            }
            log.error("[{}], #JOB#TASK_ID_BRS_ENGINEER_UNPAID#  工程师监控推送任务，执行失败 {}", TAG);
            return new Result(Action.EXECUTE_FAILED, " 工程师监控推送任务");
        } catch (Exception e) {
            log.error("[{}], #JOB#TASK_ID_BRS_ENGINEER_UNPAID#  指标推送任务失败 failed reason: {}", TAG, e.getMessage(), e);
            return new Result(Action.EXECUTE_FAILED, " 工程师监控推送任务失败");
        }
    }

    /**
     * 描述: 收款未支付金额监控
     *
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author chongw
     * @since 2021/06/28 16:12
     */
    @JobRunnerItem(shardValue = TaskConst.TASK_ID_BRS_ENGINEER_UNPAID_PUSH)
    public Result unpaidPushJob(Job job) {
        try {
            log.info("收款未支付金额====开始===");
            engineerUpaidBService.pushJob(EventTypeEnum.ENGINEER_UNPAID.getTypeId(), smsRuleId, pushTopic);
            log.info("收款未支付金额====结束===");
            return new Result(Action.EXECUTE_SUCCESS, "整体单量消息推送任务成功");
        } catch (Exception e) {
            log.error("[{}], #JOB#TASK_ID_BRS_DATA_QUANTITY_PUSH#  整体单量消息推送任务失败 failed reason: {}", TAG, e);
            return new Result(Action.EXECUTE_FAILED, " 整体单量消息推送任务失败");
        }
    }
}
